package Vista;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import Controlador.Sistema;
import DAO.DAOCombinaciones;
import DAO.DAODescuento;
import DAO.DAOReserva;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class frmConfirmarAlquiler extends javax.swing.JFrame {
	private JScrollPane jScrollPane4;
	private JLabel lblComentarios;
	private JLabel jLabel1;
	private JComboBox jComboBox1;
	static private JLabel lblDescuento;
	static private JComboBox cmbDescuentos;
	private JTextField txtComentariosCliente;
	private JButton btnConfirmar;
	private JTable tblReservas;
	private ArrayList<DAOReserva> reservas; 
	private Vector<DAOCombinaciones> descuentos = new Vector<DAOCombinaciones>();

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frmConfirmarAlquiler inst = new frmConfirmarAlquiler();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				/*{
					ComboBoxModel cmbDescuentosModel = 
						new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
					cmbDescuentos = new JComboBox();
					inst.getContentPane().add(cmbDescuentos);
					cmbDescuentos.setModel(cmbDescuentosModel);
					cmbDescuentos.setBounds(91, 228, 96, 23);
				}
				{
					lblDescuento = new JLabel();
					inst.getContentPane().add(lblDescuento);
					lblDescuento.setText("Descuento");
					lblDescuento.setBounds(16, 231, 64, 16);
				}*/

			}
		});
	}
	
	public frmConfirmarAlquiler() {
		super();
		initGUI();
		this.llenarGrillaReservas();
		llenarComboDescuentos();	
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jScrollPane4 = new JScrollPane();
				getContentPane().add(jScrollPane4);
				jScrollPane4.setBounds(12, 7, 663, 112);
				{
					TableModel tblReservasModel = 
						new DefaultTableModel(
								new String[][] { { "One", "Two" }, { "Three", "Four" } },
								new String[] { "Column 1", "Column 2" });
					tblReservas = new JTable();
					jScrollPane4.setViewportView(tblReservas);
					tblReservas.setModel(tblReservasModel);
					tblReservas.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					tblReservas.setBounds(629, 186, 66, 44);
				}
			}
			{
				btnConfirmar = new JButton();
				getContentPane().add(btnConfirmar);
				btnConfirmar.setText("Confirmar Reserva y Generar Alquiler");
				btnConfirmar.setBounds(174, 200, 310, 47);
				btnConfirmar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnConfirmarActionPerformed(evt);
					}
				});
			}
			{
				txtComentariosCliente = new JTextField();
				getContentPane().add(txtComentariosCliente);
				txtComentariosCliente.setBounds(283, 146, 392, 42);
			}
			{
				lblComentarios = new JLabel();
				getContentPane().add(lblComentarios);
				lblComentarios.setText("Comentarios del Cliente");
				lblComentarios.setBounds(283, 119, 167, 29);
			}
			{
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							new String[] {});
				jComboBox1 = new JComboBox();
				getContentPane().add(jComboBox1);
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.setBounds(12, 154, 260, 28);
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Descuento");
				jLabel1.setBounds(12, 127, 60, 22);
			}
			pack();
			this.setSize(703, 297);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void llenarGrillaReservas(){
		
		reservas = Sistema.Getinstancia().ConsultarReservas();
		
		DefaultTableModel tm = new DefaultTableModel();
		tm.addColumn("dni");
		tm.addColumn("nombre");
		tm.addColumn("apellido");
		tm.addColumn("marca");
		tm.addColumn("modelo");
		tm.addColumn("patente");
		tm.addColumn("desde");
		tm.addColumn("hasta");
		
		
		for (DAOReserva dr : reservas) {
			Vector<String> valores = new Vector<String>();
			valores.add(String.valueOf(dr.getDniCliente()));
			valores.add(dr.getNombreCliente());
			valores.add(dr.getApellidoCliente());
			valores.add(dr.getMarca());
			valores.add(dr.getModelo());
			valores.add(dr.getPatente());
			valores.add(dr.getDesde());
			valores.add(dr.getHasta());
			
			tm.addRow(valores);
			
		}
		
		this.tblReservas.setModel(tm);
		
	}	
	
	private void btnConfirmarActionPerformed(ActionEvent evt) {
		
		
		DAOReserva reserva = null;
		String comentarios = "";
		
		JTable tabla = this.tblReservas;
		
	    int registro = tabla.getSelectedRow();
	    
	    if (registro >= 0) {
	    	reserva = reservas.get(registro);
	    }
		
	    int descuentoSeleccionado = this.descuentos.elementAt(this.jComboBox1.getSelectedIndex()).getCodigo();
	    
		Sistema.Getinstancia().ConfirmarReserva(reserva, comentarios, descuentoSeleccionado);
		
		this.dispose();
		
	}
	
	private void llenarComboDescuentos()
	{
		try {
			this.descuentos = Sistema.Getinstancia().obtenerCombinacionesPosibles();
			
			
			for (DAOCombinaciones daoc : descuentos) 
			{
				this.cmbDescuentos.addItem(daoc.getDescripcion());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
